/*
 * FbShowErrorMsg.cpp
 *
 *  Created on: 2009-1-18
 *      Author: hequn
 */

#include "FbShowErrorMsg.h"
#include "ConstDefined.hrh"
#include "FbUtils.h"
//#include "Flashbox4AppUi.h"

#ifdef USE_AKNNOTEDIALOG
#include "Flashbox4_0x20021B21.rsc"
#endif


CFbShowErrorMsg* CFbShowErrorMsg::NewL(const TRect& aRect)
{
	CFbShowErrorMsg* self = CFbShowErrorMsg::NewLC(aRect);
	CleanupStack::Pop();
	return self;
}

CFbShowErrorMsg* CFbShowErrorMsg::NewLC(const TRect& aRect)
{
	CFbShowErrorMsg* self = new (ELeave) CFbShowErrorMsg;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
}

CFbShowErrorMsg::~CFbShowErrorMsg()
{
#ifdef USE_AKNNOTEDIALOG
	delete m_pMsgDialog;
	m_pMsgDialog = NULL;
#endif

	delete iPeriodic_ForDraw;
	iPeriodic_ForDraw = NULL;

	delete p_bufText;
	p_bufText = NULL;
}

void CFbShowErrorMsg::ConstructL(const TRect& aRect)
{
	CreateWindowL();
	//this->Window().SetShadowDisabled(EFalse);
	//Window().SetShadowDisabled(EFalse);
#ifdef USE_AKNNOTEDIALOG
	m_pMsgDialog = new(ELeave) CAknNoteDialog();
	_LIT(ErrorMsgTitle,"Warning!");
	//m_pMsgDialog->SetTitleL(ErrorMsgTitle);
	_LIT(ErrorMsgText,"Some Operation Wrong!");
	m_pMsgDialog->SetTextL(ErrorMsgText);
#endif
	//_LIT(Msg_Title,"Alert:");
	_LIT(Msg_Text,"System Error!");
	_LIT(Msg_Text2,"APP Shut and Exit");
	p_bufText = HBufC::New(100);//For test use, <Need modify the length, update 2009'1.19 hq>
	p_bufText->Des().Copy(Msg_Text);
//	p_bufText->Des().Append('\n');
//	p_bufText->Des().Append(Msg_Text2);

	iPeriodic_ForDraw = CPeriodic::NewL(CActive::EPriorityIdle);
	i_Timer = 0;
	i_IntervalSec = 50;//Default Interval time! <Update 2009'1.19 hq>
	SetRect(aRect);

	iPeriodic_ForDraw->Start(0, KFb_Interval, TCallBack(
			CFbShowErrorMsg::ReDrawCallback, this));
	b_CanExit = EFalse;
	
//	Has_setAlpha = EFalse;
	//MakeVisible(EFalse);

	ActivateL();
}

void CFbShowErrorMsg::SizeChanged()
{

}

TInt CFbShowErrorMsg::CountComponentControls() const
{
#ifdef USE_AKNNOTEDIALOG
	return 1;
#else
	return 0;
#endif
}

CCoeControl* CFbShowErrorMsg::ComponentControl(TInt aIndex) const
{
	switch (aIndex)
	{
#ifdef USE_AKNNOTEDIALOG
	case 0:
	return m_pMsgDialog;
#endif
	default:
		return NULL;
	}
}
const TRgb blinkRgb[5] = {TRgb(0x0, 0x0, 0xf0),
		TRgb(0x10, 0x80, 0xf0),
		TRgb(0xe0, 0x30, 0x40),
		TRgb(0xf0, 0x10, 0x20),
		TRgb(0x0, 0x0, 0x0)};

void CFbShowErrorMsg::Draw(const TRect&) const
{
	CWindowGc& gc = SystemGc();
	gc.Clear();
#ifdef USE_AKNNOTEDIALOG
	m_pMsgDialog->ExecuteLD(R_ERRORMSG_DIALOG);//LayoutAndDraw();
#else
	//gc.SetOpaque(EFalse);
	if(i_Timer == 0)
		gc.SetBrushColor(TRgb(0xff,0xff,0xff,0x80));
	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
	const CFont* font = iCoeEnv->NormalFont();
	
	gc.SetPenColor(blinkRgb[(i_Timer/4)%2]);
	gc.SetPenStyle(CGraphicsContext::ESolidPen);
	
	//Draw blink frame!
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY), TPoint(Rect().iBr.iX,Rect().iTl.iY));
	gc.DrawLine(TPoint(Rect().iBr.iX,Rect().iTl.iY), TPoint(Rect().iBr.iX,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY), TPoint(Rect().iTl.iX,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iBr.iY), TPoint(Rect().iBr.iX,Rect().iBr.iY));
	
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY+1), TPoint(Rect().iBr.iX,Rect().iTl.iY+1));
	gc.DrawLine(TPoint(Rect().iBr.iX-1,Rect().iTl.iY), TPoint(Rect().iBr.iX-1,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX+1,Rect().iTl.iY), TPoint(Rect().iTl.iX+1,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iBr.iY-1), TPoint(Rect().iBr.iX,Rect().iBr.iY-1));
	
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY+2), TPoint(Rect().iBr.iX,Rect().iTl.iY+2));
	gc.DrawLine(TPoint(Rect().iBr.iX-2,Rect().iTl.iY), TPoint(Rect().iBr.iX-2,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX+2,Rect().iTl.iY), TPoint(Rect().iTl.iX+2,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iBr.iY-2), TPoint(Rect().iBr.iX,Rect().iBr.iY-2));

	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY+3), TPoint(Rect().iBr.iX,Rect().iTl.iY+3));
	gc.DrawLine(TPoint(Rect().iBr.iX-3,Rect().iTl.iY), TPoint(Rect().iBr.iX-3,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX+3,Rect().iTl.iY), TPoint(Rect().iTl.iX+3,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iBr.iY-3), TPoint(Rect().iBr.iX,Rect().iBr.iY-3));

	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iTl.iY+4), TPoint(Rect().iBr.iX,Rect().iTl.iY+4));
	gc.DrawLine(TPoint(Rect().iBr.iX-4,Rect().iTl.iY), TPoint(Rect().iBr.iX-4,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX+4,Rect().iTl.iY), TPoint(Rect().iTl.iX+4,Rect().iBr.iY));
	gc.DrawLine(TPoint(Rect().iTl.iX,Rect().iBr.iY-4), TPoint(Rect().iBr.iX,Rect().iBr.iY-4));
	
	gc.SetPenColor(blinkRgb[4/*(i_Timer/4)%5*/]);
	gc.UseFont(font);
	gc.DrawText(p_bufText->Des(), Rect(), Rect().iTl.iY + 30,
			CGraphicsContext::ECenter);
	/*If app need Exit, we can draw below text*/	
	TBuf16<40> str;
	str.Format(_L("Shut App:  %d"),(i_IntervalSec-i_Timer)/10);
	gc.SetPenColor(blinkRgb[(i_Timer/4)%2+2]);
	gc.DrawText(str, Rect(), Rect().iTl.iY + 50,
			CGraphicsContext::ECenter);
	/*-----------------------------------------*/
	gc.DiscardFont();
#endif

}

void CFbShowErrorMsg::SetMsgTitle(const TDesC& aTitle)
{

}

void CFbShowErrorMsg::SetMsgContent(const TDesC& aText)
{
	*p_bufText = aText;
}

void CFbShowErrorMsg::SetInterval(const TInt& aSec)
{
	i_IntervalSec = aSec;
}

TInt CFbShowErrorMsg::ReDrawCallback(TAny* pInst)
{
	CFbShowErrorMsg* pThis = (CFbShowErrorMsg*) pInst;
	pThis->i_Timer++;
	
	if(pThis->i_Timer > pThis->i_IntervalSec)
	{
		pThis->iPeriodic_ForDraw->Cancel();
		if (pThis->b_CanExit)
			pThis->ExitApp();
	}
	else
	{
		if((pThis->i_Timer%4) == 0)
			pThis->DrawNow();
	}
}

void CFbShowErrorMsg::ExitApp()
{
	GetAppUi()->Exit();
}
